<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form class="tabelForm" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所在仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" placeholder="请选择所在仓库" clearable size="small">
          <el-option v-for="warehouse in warehouseList" :key="warehouse.id" :label="warehouse.name"
                     :value="warehouse.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="原编码" prop="oldRfidCode">
        <el-input v-model="queryParams.oldRfidCode" placeholder="请输入原RFID编码" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="新编码" prop="newRfidCode">
        <el-input v-model="queryParams.newRfidCode" placeholder="请输入新RFID编码" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="更换原因" prop="changeReason">
        <el-select v-model="queryParams.changeReason" placeholder="请输入更换原因" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.CHANGE_REASON)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="更换状态" prop="changeStatus">
        <el-select v-model="queryParams.changeStatus" placeholder="请选择更换状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.CHANGE_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>

      <el-form-item label="更换人" prop="changeUser">
        <el-select v-model="queryParams.changeUserId" placeholder="请输入更换人" clearable size="small">
          <el-option v-for="user in userList" :key="user.id" :label="user.nickname"
                     :value="user.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="分派人" prop="assignUserId">
        <el-select v-model="queryParams.assignUserId" placeholder="请输入更换人" clearable size="small">
          <el-option v-for="user in userList" :key="user.id" :label="user.nickname"
                     :value="user.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="更换时间" prop="changeTime">
        <el-date-picker v-model="queryParams.changeTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <div class="toolbarBox">
      <div class="left-toolbar">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="medium"
          @click="openForm(undefined)"
          v-hasPermi="['hs:rfid-change-record:create']"
        >更换标签
        </el-button>
        <el-button
          type="default"
          icon="el-icon-download"
          size="medium"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['hs:rfid-change-record:export']"
        >导出
        </el-button>
        <div class="toolsbtn">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </div>

      </div>
      <div class="right-toolbar">
        <!-- 分页组件 -->
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
          layout="prev, pager, next"
          :background="false"
        />
      </div>
    </div>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column label="所在仓库" align="center" prop="warehouseId" :formatter="warehouseFormat"/>
      <el-table-column label="原RFID编码" align="center" prop="oldRfidCode"/>
      <el-table-column label="新RFID编码" align="center" prop="newRfidCode"/>
      <el-table-column label="更换原因" align="center" prop="changeReason">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.CHANGE_REASON" :value="scope.row.changeReason"/>
        </template>
      </el-table-column>
      <el-table-column label="更换状态" align="center" prop="changeStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.CHANGE_STATUS" :value="scope.row.changeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="更换时间" align="center" prop="changeTime" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.changeTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更换人" align="center" prop="changeUserName"/>
      <el-table-column label="分派人" align="center" prop="assignUserName"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-refresh" @click="openForm(scope.row.id)"
                     v-hasPermi="['hs:rfid-change-record:update']" v-if="scope.row.changeStatus === 0">更换标签
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['hs:rfid-change-record:delete']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 对话框(添加 / 修改) -->
    <RFIDChangeRecordForm ref="formRef" @success="getList"/>
  </div>
</template>

<script>
import * as RfidChangeRecordApi from '@/api/hs/rfidchangerecord';
import RFIDChangeRecordForm from './RFIDChangeRecordForm.vue';
import {listSimpleUsers} from "@/api/system/user";
import {getWarehouseSimpleList} from "@/api/hs/warehouse";
import store from "@/store";

export default {
  name: "RFIDChangeRecord",
  components: {
    RFIDChangeRecordForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // RFID更换记录列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        warehouseId: null,
        oldRfidCode: null,
        newRfidCode: null,
        changeReason: null,
        changeStatus: null,
        changeTime: [],
        changeUserId: null,
        changeUserName: null,
        assignUserId: null,
        assignUserName: null,
        createTime: [],
      },
      warehouseList: [],
      userList: [],
    };
  },
  created() {
    this.getList();
    this.getWarehouseList();
    this.getUserList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await RfidChangeRecordApi.getRfidChangeRecordPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除RFID更换记录编号为"' + id + '"的数据项?')
      try {
        await RfidChangeRecordApi.deleteRfidChangeRecord(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有RFID更换记录数据项?');
      try {
        this.exportLoading = true;
        const data = await RfidChangeRecordApi.exportRfidChangeRecordExcel(this.queryParams);
        this.$download.excel(data, 'RFID更换记录.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
    /** 查询仓库列表 */
    async getWarehouseList() {
      getWarehouseSimpleList().then(res => {
        this.warehouseList = res.data;
      })
    },
    /** 查询当前登录人所在仓库的用户列表 */
    async getUserList() {
      let query = {
        warehouseId: store.getters.warehouseId,
      };
      listSimpleUsers(query).then(res => {
        this.userList = res.data;
      })
    },
    // 仓库名称展示
    warehouseFormat(row, column) {
      if (!row.warehouseId) {
        return '未设置';
      }
      for (const warehouse of this.warehouseList) {
        if (row.warehouseId === warehouse.id) {
          return warehouse.name;
        }
      }
      return '未知【' + row.warehouseId + '】';
    },
  }
};
</script>
